<?php

/**
* indexController.php
*
* 管理员主页类 
*
* @author witiq <微信号：937731903>
* @version 1.0
* @package controller
*/

class adminController extends Controller {

	/**
	 *@var adminService
	 */
	protected  $adminService ;

	/**
	 *@var 定义menu按钮
	 */
	protected $menu_buttons = array('add','delete','verify','order');

	/**
	 * curd  所对应的方法名 操作的页面地址
	 */
	protected  $curd = array('c'=>'add','u'=>'edit','r'=>'view','d'=>'delete','o'=>'order');
	protected  $curd_url;
	/**
	 *@var ownService 
	 */ 
	protected $ownService;
	/**
	 * [$table_key description]
	 * @var [type]
	 */
	protected $table_key;
	/**
	 * [$service_name description]
	 * @var [type]
	 */
	protected $service_name;

	/**
	 *  @var  fields
	 */
	protected $view_fields     = array();
	protected $select_fields   = array();	
	protected $checkbox_fields = array();	
	protected $function_fields = array();
	protected $treelist_fields = array();
	protected $preset_fields   = array();
	protected $title = 'admin';
	protected $diy_tpl = 0;
	/**
	 * [$load_js description]
	 * @var array
	 */
	protected $load_js = array('upload'=>0,'tag'=>0 ,'date'=>0,'autoinput'=>0);

	public function __construct(){
		parent::__construct();
		$this->adminService = InitPHP::getService('admin');
		$this->ownService = InitPHP::getService($this->service_name);
		$this->table_key = $this->ownService->getTableKey();
		$this->setCurd();
		$this->config();
	}

	/**
	 * [configFields description]
	 * @return [type] [description]
	 */
	protected function config(){

	}
	//公用视图部分

	/**
	 * 列表页面
	 * @param  
	 * @type  page
	 */
	public function manage(){
	    $this->doAdminAuth();
	    //获取列表
	    $page = isset($_GET['page']) ? intval($this->controller->get_gp('page')): 1;
	    $pagesize = 10;
	    $fields= $this->getSearchFields();

	    $page_data = $this->ownService->getModelList($fields,$page,$pagesize);
	    $page_num = $this->ownService->getModelNum($fields);
	    $page_html= $this->getLibrary('pager')->pager($page,$page_num,$pagesize); 
	    
		$this->setViewFields();		
	    $this->view->assign('page_html',$page_html);
	    $this->view->assign('view_list',$page_data['list']);  
	    $this->view->assign('menu_buttons',$this->menu_buttons);
		$tpl = $this->diy_tpl ? $this->service_name : 'common_list';
		$this->view->set_tpl('admin/'.$tpl);
		$this->view->display();
	}
	
	/**
	 * 添加页面
	 * @param   $_POST['user']
	 * @type  page
	 */
	public function add(){
		$this->doAdminAuth();
		$this->setViewFields();
		$tpl = $this->diy_tpl ? $this->service_name : 'common_add';
		$this->view->set_tpl('admin/'.$tpl);
		$this->view->display();
	}
	/**
	 * 编辑
	 * @param  
	 * @type  page
	 */  
	public function edit(){
		$this->doAdminAuth();
		$id = isset($_GET[$this->table_key]) ? intval($this->controller->get_gp($this->table_key)) : exit('');
		$model  = $this->ownService->getModel($id);
		$this->setViewFields();
		$this->view->assign('model',$model);
		$tpl = $this->diy_tpl ? $this->service_name : 'common_edit';
		$this->view->set_tpl('admin/'.$tpl);
		$this->view->display();
	}

	/**
	 * 查看方法 ajax
	 */
	public function view(){
		$this->doAdminAuth();
		$id = isset($_GET[$this->table_key]) ? intval($this->controller->get_gp($this->table_key)) : exit('');
		$model  = $this->ownService->getModel($id);
		$this->setViewFields();
		$this->view->assign('model',$model);
		$tpl = $this->diy_tpl ? $this->service_name : 'common_view';
		$this->view->set_tpl('admin/'.$tpl);
		$this->view->display();
	}
	/**
	 * 查看方法
	 */
	public function order(){
		$this->doAdminAuth();
		$ids  = isset($_POST['ids']) ? $this->controller->get_gp('ids'):exit('');
		$idArr = explode(',', $ids);
		foreach($idArr  as $order => $id){
			$this->ownService->updateModelByFields(array($this->function_fields['order_field']=>$order),array($this->table_key=>$id));
		} 
		$this->controller->ajax_return(0,'排序成功');
	}

	/**
	 * 添加请求
	 * @param  $_POST['user']
	 * @type   post
	 */
	public function addPost(){
		$this->doAdminAuth();
		$r = $this->ownService->createModel($this->getFormData());
		if(isset($r['error'])){
			$this->tip($r);
		}
		$this->forward($this->service_name,'manage');	
	}

	/**
	 * 编辑请求
	 * @param  $_POST['rank']
	 * @type   post
	 */
	public function editPost(){
		$this->doAdminAuth();
		$r = $this->ownService->updateModel($this->getFormData(true));
		if(isset($r['error'])){
			$this->tip($r);
		}
		$this->forward($this->service_name,'manage');
	}

	/**
	 * 删除数据接口
	 * ajax
	 * @param $_POST['id'] 
	 */
	public function delete(){
		$id = isset($_POST[$this->table_key]) ? intval($this->controller->get_gp($this->table_key)) : -1;
		$r = $this->ownService->deleteModel($id);
		if(isset($r['error'])){
			$this->controller->ajax_return($r['error'],$r['msg']);
		}
		$this->controller->ajax_return(0,'删除成功');
	}

	
	/**
	 * 控制器重定向
	 * Controller使用方法 :$this->controller->forward($url,$time =0)
	 * 	@param  string  $c   跳转的方法
	 * 
	 * 	@param  string  $a   跳转的方法
	 * 	@param  int     $time  多少秒后跳转
	 */
	public function forward($c,$a,$time=0) {
		$url = 'index.php?m='.InitPHP::getM().'&c='.$c.'&a='.$a.'&init_token='.$this->controller->get_token();
		$this->controller->redirect($url);
		exit();
	}

	/**
	 * 调整到提示页面
	 * @param message  $array('error'=>'','msg'=>'msg')
	 * @param 
	 */
	public function tip($message,$forward=''){
		$this->view->assign('message',$message);
		$this->view->assign('menu',InitPHP::getService('menu')->getMenu());
		$this->view->set_tpl('msg');
		$this->view->display();
		exit();
	}


	/**
	 * 上传文件接口
	 */
	public function uploadFile(){
		$fileupload= $this->getLibrary('fileupload'); //文件上传类加载
		$fields = $this->controller->get_gp('imagefield');
		$filename = md5(microtime());
		$path   = UPLOAD_IMAGE_PATH . '/'. date('Ymd');
		$allow_type = array('pem','exe','zip');
        $upload   = $fileupload->up($filefield,$path,$filename,$allow_type);
        $this->controller->ajax_return($upload['error'],$upload['msg'],$upload['file']);
        
	}

	/**
	 * 上传图片接口
	 */
	public function uploadImage(){
		$imageupload= $this->getLibrary('fileupload'); //文件上传类加载
		$fields = $this->controller->get_gp('filefield');
		$imagename = md5(microtime());
		$path   = UPLOAD_FILE_PATH . '/'. date('Ymd');
		$allow_type = array('png','jpg','jpeg');
        $upload   = $imageupload->up($filefield,$path,$imagename,$allow_type);
        $this->controller->ajax_return($upload['error'],$upload['msg'],$upload['file']);
        
	}


	/**
	 * 设置控制器curd对应链接
	 * @param array('c'=>'add','u'=>'edit','r'=>'view','d'=>'delete')
	 */
	protected function setCurd(){
		$m = InitPHP::getM();
		$c = InitPHP::getC();
		$a = InitPHP::getA();
		$curd = $this->curd;
		$this->curd_url = array('c'=>"index.php?m=$m&c=$c&a=$curd[c]",
							'u'=>"index.php?m=$m&c=$c&a=$curd[u]",
							'r'=>"index.php?m=$m&c=$c&a=$curd[r]",
							'd'=>"index.php?m=$m&c=$c&a=$curd[d]",
							'o'=>"index.php?m=$m&c=$c&a=$curd[o]",	
							);
	}

	/**
	 * [getSerachFields 获取搜索字段的键值对]
	 * @return [array] [搜索字段的键值对]
	 */
	protected function getSearchFields(){
	    $fields= array(); 
	   	foreach($this->select_fields as $field =>$v){
	   		$fieldval = isset($_POST[$field])?intval($this->controller->get_gp($field)) : -1;
	   		if($fieldval > -1 ){
	   			$fields[$field] = $fieldval;
	   			
	   		}
	   	} 
	   	
	   	if(!empty($_POST['keyword'])){
	   		$keyword = $this->controller->get_gp('keyword');
   			$fields[$this->function_fields['search']] = array('like'=>"%$keyword%");
	   	}
	   	return $fields;
	}

	/**
	 * @param  [bool] is_edit 是否为提交表单
	 * @return [array] $model 表单键值对
	 */
	protected function getFormData($is_edit = false){

		$model = array();
		foreach($this->view_fields as $field=>$val){
			if($val['type'] == 'key' && !$is_edit){
				continue;
			}
			if($val['type'] == 'checkbox' || $val['type'] == 'tag' ){
				$model[$field] = implode(',', (array)$this->controller->get_gp($field));

			}else if ($val['type'] == 'autoinput'){	
				 $arr = explode('(', $this->controller->get_gp($field) );
				 $model[$field]	= $arr[0];
		    }else if ($val['type'] == 'richtext'){	
		    	 $temp = $this->controller->filter_sql($_POST[$field]);	 
		    	 $model[$field] =$this->controller->filter_script($temp);	    	 
			}else{
				$model[$field] = $this->controller->get_gp($field); 
			}
		}
		return $model;
	}

	/**
	 * 设置视图
	 */
	protected function setViewFields(){
		$this->view->assign('preset_data',$this->preset_fields);
		$this->view->assign('load_js',$this->load_js);
		$this->view->assign('curd_url',$this->curd_url);		
		$this->view->assign('table_key',$this->table_key);
		$this->view->assign('function_fields',$this->function_fields);
		$this->view->assign('view_fields',$this->view_fields);   
	    $this->view->assign('select_fields',$this->select_fields);
	    $this->view->assign('checkbox_fields',$this->checkbox_fields);
	    $this->view->assign('treelist_fields',$this->treelist_fields);
	    $this->view->assign('title',$this->title);
		$this->view->assign('menu',InitPHP::getService('menu')->getMenu());
	}

	/**
	 * 验证管理员登录
	 */
	public function doAdminAuth(){
		$admin = $this->adminService->authAdminLoginSession();
		if($admin){
			$this->view->assign('admin',$admin);
			return true;
		}
		$this->forward('index','login'); 
	}

}